<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form"  :inline="true" :model="form" label-width="140px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="医生姓名">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="医院名称">
              <el-select filterable v-model="form.hospitalId" placeholder="请选择">
                <el-option
                  v-for="item in officeOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="显示状态">
              <el-select v-model="form.status" placeholder="请选择">
                <el-option
                  v-for="item in statusOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否推荐">
              <el-select v-model="form.isRecommend" placeholder="请选择">
                <el-option
                  v-for="item in isRecommendOption"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <!-- <el-form-item> -->
            <el-button style="margin-left:60px" type="primary" @click="searchFn">查询</el-button>
            <el-button
              v-if="hasPermission('AdminDoctorProvider.createDoctor')"
              @click="toRoutes('doctorAdd')"
              type="primary"
            >新增</el-button>
            <!-- </el-form-item> -->
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <!-- table列表 -->
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column prop="sort" label="排序"></el-table-column>
        <el-table-column prop="name" label="医生姓名"></el-table-column>
        <el-table-column prop="hospitalName" label="医院名称"></el-table-column>
        <el-table-column prop="post" label="职位"></el-table-column>
        <el-table-column label="学历职称">
          <template slot-scope="scope">
            <div v-for="(i,index) in scope.row.tags" :key="index">
              <span>{{i}}</span>
              <br />
            </div>
          </template>
        </el-table-column>
        <el-table-column label="医生照片">
          <template slot-scope="scope">
            <div>
              <img style="height:40px;width:40px" :src="scope.row.image | imgUrlFn" alt />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="star" label="评星"></el-table-column>
        <el-table-column label="显示状态">
          <template slot-scope="scope">
            <el-switch
              class="switchStyle"
              v-model="scope.row.status"
              @click.native="changeStatus(scope.row.id,'status',scope.row.status)"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="0"
              :inactive-value="1"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否推荐">
          <template slot-scope="scope">
            <el-switch
              class="switchStyle"
              v-model="scope.row.isRecommend"
              @click.native="changeStatus(scope.row.id,'isRecommend',scope.row.isRecommend)"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="是否可预约">
          <template slot-scope="scope">
            <el-switch
              class="switchStyle"
              v-model="scope.row.isAppoint"
              @click.native="changeStatus(scope.row.id,'isAppoint',scope.row.isAppoint)"
              active-color="#13ce66"
              inactive-color="#eee"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createdAt" label="添加时间"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              v-if="hasPermission('AdminDoctorProvider.updateDoctor')"
              type="text"
              @click="toRoutes('doctorEdit',scope.row.id)"
              size="small"
            >编辑</el-button>
            <el-button
              v-if="hasPermission('AdminDoctorProvider.doctorDetail')"
              type="text"
              @click="toRoutes('doctorInfo',scope.row.id,0)"
              size="small"
            >详情</el-button>
            <el-button
              v-if="hasPermission('AdminDoctorProvider.deleteDoctor')"
              type="text"
              @click="deleteFn(scope.row.id)"
              size="small"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Tinymce style="display:none" />
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listForm.pageNum"
            :page-sizes="page_sizes"
            :page-size="listForm.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  doctorList,
  changeDoctorStatus,
  deleteDoctor
} from "@/api/doctorManage";
import Tinymce from "@/components/Tinymce";
import { companyList } from "@/api/branchOfficeDropDown";
import { hospitalDropDown } from "@/api/doctorManage";
import { policy } from "@/api/policy";

export default {
  name: "doctorIndex",
  components: { Tinymce },
  data() {
    return {
      imgUrl: "",
      tableLoading: true,
      officeOption: [],
      statusOption: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "正常"
        },
        {
          value: "1",
          label: "停用"
        }
      ],
      isRecommendOption: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "不推荐"
        },
        {
          value: "1",
          label: "推荐"
        }
      ],
      form: {
        name: "",
        hospitalId: "",
        status: "",
        isRecommend: "",
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      page_sizes: [10, 30, 50, 100],
      page_totals: 60,
      listForm: {
        pageNum: 1,
        pageSize: 10
      }
    };
  },
  filters: {
    imgUrlFn: function(e) {
      return localStorage.getItem("imgUrlPrefix") + e;
    }
  },
  mounted() {
    //分公司列表
    this.getCompanyList();
    this.getList(this.listForm);
  },
  methods: {
    //按钮权限
    hasPermission(e) {
      return window.helper.hasPermission(e);
    },
    //上下架与状态修改
    changeStatus(id, type, status) {
      let datas = {
        id: id + ""
      };
      datas[type] = status + "";
      changeDoctorStatus(datas).then(res => {
        // console.log(res)
        if (res.data.code === 0) {
          this.$message({
            message: "修改成功",
            type: "success"
          });
        } else {
          this.$message.error("修改失败");
        }
      });
      // console.log(datas)
    },
    //获取分公司列表
    getCompanyList() {
      // 医院列表
      hospitalDropDown().then(res => {
        let resData = res.data.data;
        let newData = [];
        resData.forEach(e => {
          newData.push({
            value: e.id + "",
            label: e.name
          });
        });
        newData.unshift({
          value: "",
          label: "全部"
        });
        this.officeOption = newData;
        // console.log(this.officeOption)
      });
      // companyList().then(res =>{
      //     let resData = res.data.data
      //     let newData = [];
      //     resData.forEach(e => {
      //         newData.push({
      //             value:e.id,
      //             label:e.name
      //         })
      //     });
      //     this.officeOption = newData
      // })
      // .catch(error =>{
      //     this.$message.error(error);
      // })
    },
    //成功状态弹窗
    succStatus(txt) {
      this.$message({
        message: txt + "成功",
        type: "success"
      });
    },
    //失败状态弹窗
    errorStatus(txt) {
      this.$message.error(txt + "失败");
    },
    //查询
    searchFn() {
      this.form.pageNum = 1;
      this.form.hospitalId = this.form.hospitalId + "";
      // console.log(this.form)
      this.getList(this.form);
    },
    // 路由跳转
    toRoutes(e, userId, num) {
      let route = { name: e, query: { userId: userId, num: num } };
      this.$router.push(route);
    },
    //列表数据获取
    getList(e) {
      this.tableLoading = true;
      let datas = e;
      doctorList(datas)
        .then(res => {
          console.log(res);
          if (!res.data.code) {
            // console.log(res)
            this.tableData = res.data.data.list;
            this.page_totals = res.data.data.count;
            if (this.form.hospitalId != "") {
              this.form.hospitalId = this.form.hospitalId;
            }
          } else {
            this.$message.error(res.data.msg);
          }
          this.tableLoading = false;
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    //删除
    deleteFn(e) {
      let datas = {
        id: e
      };
      this.$confirm("删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteDoctor(datas)
            .then(res => {
              console.log(res);
              if (!res.data.code) {
                this.succStatus("删除");
                this.getList(this.form);
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(error => {
              this.$message.error(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },
    //选择页数
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getList(this.form);
    }
  }
};
</script>
<style scoped>
/* 分页 */
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
</style>
